package com.dianrui.agent.activity;

import android.view.View;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.FinanceList;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TodayFinaceInfoActivity extends BaseActivity {

    private List<FinanceList> finaceListAll = new ArrayList<>();
    private String mToday;
    private String ordercount;
    private String year;
    private String month;
    private String day;
    private String areaoutside;
    private String networkside;
    private String moneys;
    private String discounts;
    private String wxfee;
    private String alifee;
    private String totals;


    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.order_count)
    TextView orderCount;
    @BindView(R.id.network_outside)
    TextView networkOutside;
    @BindView(R.id.area_outside)
    TextView areaOutside;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.discount)
    TextView discount;
    @BindView(R.id.wx_payfee)
    TextView wxPayfee;
    @BindView(R.id.ali_payfee)
    TextView aliPayfee;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.no_pay)
    TextView noPay;
    @BindView(R.id.overtime_payment)
    TextView overtimePayment;


    @Override
    public int getContentViewId() {
        return R.layout.activity_todayfiance_info;
    }

    @Override
    public void initView() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        mToday = simpleDateFormat.format(date);
        topRight.setVisibility(View.VISIBLE);
        topRight.setText("历史记录");
        requestFinance();
    }


    private void requestFinance() {
        JsonObject finaceListJson = new JsonObject();
        finaceListJson.addProperty("start_time", mToday);
        finaceListJson.addProperty("end_time", mToday);
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.FINANICE_LIST, finaceListJson.toString(), new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result) && !isFinishing()) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            if (finaceListAll != null) {
                                finaceListAll.clear();
                            }
                            JSONArray jsonArray = jsonObject.optJSONArray("data");
                            if (jsonArray.length() > 0) {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    FinanceList financeList = new FinanceList();
                                    financeList.month = jsonArray.optJSONObject(i).optString("month");
                                    financeList.create_time = jsonArray.optJSONObject(i).optString("create_time");
                                    financeList.day = jsonArray.optJSONObject(i).optString("day");
                                    financeList.year = jsonArray.optJSONObject(i).optString("year");
                                    financeList.money = jsonArray.optJSONObject(i).optString("money");
                                    financeList.discount = jsonArray.optJSONObject(i).optString("discount");
                                    financeList.total = jsonArray.optJSONObject(i).optString("total");
                                    financeList.network_outside = jsonArray.optJSONObject(i).optString("network_outside");
                                    financeList.pay_fee = jsonArray.optJSONObject(i).optString("pay_fee");
                                    financeList.wx_pay_fee = jsonArray.optJSONObject(i).optString("wx_pay_fee");
                                    financeList.ali_pay_fee = jsonArray.optJSONObject(i).optString("ali_pay_fee");
                                    financeList.no_pay = jsonArray.optJSONObject(i).optString("no_pay");
                                    financeList.overtime_payment = jsonArray.optJSONObject(i).optString("overtime_payment");
                                    financeList.area_outside = jsonArray.optJSONObject(i).optString("area_outside");
                                    financeList.rate_price = jsonArray.optJSONObject(i).optString("rate_price");
                                    financeList.order_count = jsonArray.optJSONObject(i).optString("order_count");
                                    finaceListAll.add(financeList);
                                }
                                title.setText(finaceListAll.get(0).create_time);
                                orderCount.setText(finaceListAll.get(0).order_count);
                                networkOutside.setText(finaceListAll.get(0).network_outside);
                                areaOutside.setText(finaceListAll.get(0).area_outside);
                                money.setText(finaceListAll.get(0).money);
                                discount.setText(finaceListAll.get(0).discount);
                                wxPayfee.setText(finaceListAll.get(0).wx_pay_fee);
                                aliPayfee.setText(finaceListAll.get(0).ali_pay_fee);
                                total.setText(finaceListAll.get(0).total);
                                noPay.setText(finaceListAll.get(0).no_pay);
                                overtimePayment.setText(finaceListAll.get(0).overtime_payment);
                            }
                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        }else if ("1002".equals(jsonObject.optString("status"))) {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick({R.id.back, R.id.top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.top_right:
                JumpActivitys(StatementAccountsActivity.class);
                break;
                default:
                    break;
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (finaceListAll != null) {
            finaceListAll.clear();
        }
    }
}
